package com.xhb.ui.collect;

import static com.teamdev.jxbrowser.engine.RenderingMode.HARDWARE_ACCELERATED;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.nio.file.Paths;
import java.util.UUID;

import javax.swing.JButton;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;

import org.noear.solon.Solon;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.teamdev.jxbrowser.engine.Engine;
import com.teamdev.jxbrowser.engine.EngineOptions;
import com.teamdev.jxbrowser.engine.ProprietaryFeature;
import com.xhb.EasyBrowserApplication;
import com.xhb.biz.po.CollectUrlPO;
import com.xhb.biz.service.CollectUrlService;
import com.xhb.ui.ImgGenFrame;
import com.xhb.ui.Tab;
import com.xhb.ui.TabCaption;
import com.xhb.ui.TabFactory;
import com.xhb.ui.http.HttpServer;
import com.xhb.ui.tools.UItools;

import cn.hutool.http.HttpUtil;
import lombok.AllArgsConstructor;
import lombok.Data;

@Data
public class CollectUrlButton extends JButton {

	private CollectUrlPO collectUrlPO;

	public CollectUrlButton(CollectUrlPO collectUrlPO, CollectBar collectBar) {
		this.collectUrlPO = collectUrlPO;
		UItools.parseButton(this);
		setBackground(Color.WHITE);
		setOpaque(true);
		final JButton jButton = this;
		addMouseListener(new MouseAdapter() {
			@Override
			public void mouseEntered(MouseEvent e) { // 鼠标移上去
				jButton.setBackground(Color.lightGray);
			}
		});
		addMouseListener(new MouseAdapter() {
			@Override
			public void mouseExited(MouseEvent e) { // 鼠标离开
				jButton.setBackground(Color.WHITE);
			}
		});
		addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (StringUtils.equals("ImgGen", collectUrlPO.getUrl())) {
					ImgGenFrame imgGenFrame = new ImgGenFrame();
					imgGenFrame.setVisible(true);
				} else if (StringUtils.equals("examjx", collectUrlPO.getUrl())) {
					
					EngineOptions options = EngineOptions.newBuilder(HARDWARE_ACCELERATED).userDataDir(Paths.get(HttpServer.BASE_PATH+"/p/"+UUID.randomUUID().toString()))
							.enableProprietaryFeature(ProprietaryFeature.AAC).enableProprietaryFeature(ProprietaryFeature.H_264)
							.enableProprietaryFeature(ProprietaryFeature.WIDEVINE)
							.addSwitch("--remote-allow-origins=http://127.0.0.1:9222  --allow-file-access-from-files").remoteDebuggingPort(9222).enableIncognito().disableWebSecurity().
							allowFileAccessFromFiles()
							.build();
					Engine engine = Engine.newInstance(options);
					
					engine.newBrowser().mainFrame().ifPresent(frame -> {
						String url = Solon.cfg().get("easy-borwser.server-url") + "js/subject.js";
						// String script = HttpUtil.get(url);
						String js="$.getScript('" + url + "', function() {\n" + " console.log('success')" + "});";
						frame.executeJavaScript(js
								);
					});
				} else {

					EngineOptions options = EngineOptions.newBuilder(HARDWARE_ACCELERATED).userDataDir(Paths.get(HttpServer.BASE_PATH+"/p/"+UUID.randomUUID().toString()))
							.enableProprietaryFeature(ProprietaryFeature.AAC).enableProprietaryFeature(ProprietaryFeature.H_264)
							.enableProprietaryFeature(ProprietaryFeature.WIDEVINE)
							.addSwitch("--remote-allow-origins=http://127.0.0.1:9222  --allow-file-access-from-files").remoteDebuggingPort(9222).enableIncognito().disableWebSecurity().
							allowFileAccessFromFiles()
							.build();
					Engine engine = Engine.newInstance(options);

					EasyBrowserApplication.insertTab(TabFactory.createTab(collectUrlPO.getUrl(), engine.newBrowser()));
				}
			}
		});
		addMouseListener(new UrlPopMenu(collectUrlPO, collectBar, this));
	}

	@AllArgsConstructor
	public static class UrlPopMenu extends MouseAdapter {

		private CollectUrlPO collectUrlPO;
		private CollectBar collectBar;

		private JButton jButton;

		public void mousePressed(MouseEvent e) {
			int mods = e.getModifiers();
			if ((mods & InputEvent.BUTTON3_MASK) != 0) {
				JPopupMenu popupMenu = new JPopupMenu();
				JMenuItem delete = new JMenuItem();
				delete.setText("删除");
				delete.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						int opt = JOptionPane.showConfirmDialog(null, "确认删除" + collectUrlPO.getName() + "吗？", "请确认",
								JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
						if (opt == JOptionPane.YES_NO_OPTION) {
							CollectUrlService collectUrlService = Solon.context().getBean(CollectUrlService.class);
							collectUrlService.removeById(collectUrlPO.getId());
							if (jButton != null) {
								jButton.setVisible(false);
							}
							collectBar.refresh();
						}
					}
				});

				popupMenu.add(delete);
				JMenuItem edit = new JMenuItem();
				edit.setText("编辑");
				edit.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						UpdateCollectUrlDialog updateCollectUrlDialog = new UpdateCollectUrlDialog(collectUrlPO,
								collectBar, false);
						updateCollectUrlDialog.setModal(true);
						updateCollectUrlDialog.setVisible(true);
						if (jButton != null) {
							jButton.setVisible(false);
						}
						collectBar.refresh();
					}
				});

				popupMenu.add(edit);
				popupMenu.show(e.getComponent(), e.getX(), e.getY());
			}
		}
	}
}
